#include <cstdio>
#include <vector>
using namespace std;

class Node;
class Edge
{
public:
	int value;
	Node* target;

	Edge( int v, Node* t ): value(v), target(t)
	{
	}

};

class Node
{
public:
	vector<Edge> adj;
	int maxv;
	bool used;

	Node()
	{
		used = false;
		maxv = 0;
	}
	
	void Add( int v, Node* t )
	{
		adj.push_back( Edge( v, t ) );
	}
	
};

class Problem
{
	vector<Node> nodes;
	int count;
	int root;

public:

	void Reset( int c, int r )
	{
		count = c;
		root = r;
		nodes.resize(c);
	}
	
	void AddEdge( int from, int to, int value )
	{
		nodes[from].Add( value, &nodes[to] );
		nodes[to].Add( value, &nodes[from] );
	}
	
	void TravelFind( Node& r )
	{
		r.used = true;
		int maxv = 0;
		for( vector<Edge>::const_iterator it = r.adj.begin(); it != r.adj.end(); it++ )
		{
			const Edge& e = *it;
			if( e.target->used ) continue;
			TravelFind( *e.target );
			int v = e.target->maxv + e.value;
			if( maxv < v ) maxv = v;
		}
		r.maxv = maxv;
	}
	
	void TravelSupply( Node& r, int mv, int* ptotal )
	{
		r.used = true;
		int cost = mv - r.maxv;
		*ptotal += cost;
		for( vector<Edge>::const_iterator it = r.adj.begin(); it != r.adj.end(); it++ )
		{
			const Edge& e = *it;
			if( e.target->used ) continue;
			TravelSupply( *e.target, mv - e.value - cost, ptotal );
		}
	}
	
	int Slove()
	{
		TravelFind( nodes[root] );
		for( vector<Node>::iterator it = nodes.begin(); it != nodes.end(); it++ )
		{
			it->used = false;
		}
		int total = 0;
		TravelSupply( nodes[root], nodes[root].maxv, &total );
		return total;
	}
};

int main()
{
	while( 1 )
	{
		int c = 0, r = 0;
		scanf( "%d %d", &c, &r );
		if( c == 0 && r == 0 ) break;
		Problem pro;
		pro.Reset( c, r );
		for( int i = 0; i < c - 1; i++ )
		{
			int from = 0, to = 0, value = 0;
			scanf( "%d %d %d", &from, &to, &value );
			pro.AddEdge( from, to, value );
		}
		printf( "%d\n", pro.Slove() );
	}
	return 0;
}